package com.huarui.partner.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;
import com.huarui.partner.model.PartnerAddress;
import com.huarui.partner.service.PartnerAddressService;
import com.huarui.util.ConstantUtil;
/**
 * xzz
 * @author Administrator
 *
 */
public class PartnerAddressAction extends BaseAction {

	private static final long serialVersionUID = -4662994553326859620L;
    private static final Logger log = Logger.getLogger(PartnerAddressAction.class);
	private Integer partnerId;
	private Integer start;
	private Integer limit;
	private String queryParam;
	private PartnerAddressService partnerAddressService;
	private JSONObject jsonResult;
	private Map<String,Object> resultMap=new HashMap<String, Object>();

	public String query() throws IOException {
		if (partnerId == null) {
			
			 resultMap.put("failture", "客户不存在");
		    	
			 return "json";
		
		}
		List<PartnerAddress> partnerAddressList = partnerAddressService.getPartnerAddressByPartnerId(partnerId);
		JSONArray root = JSONArray.fromObject(partnerAddressList);
		// 分页信息
		resultMap.put("root", root);

		return "json";
	}
    /**
     * 根据partnerId,查询关键字查询该partner的所有收货地址
     * @return
     * @author yi
     */
	@SuppressWarnings("unchecked")
	public String queryPartnerAddressImplicit() {
		if (partnerId == null) {
			throw new RuntimeException();
		}
		ListModel<PartnerAddress> padListModel = generateListModelParams();
		ListModel<PartnerAddress> partnerAddressResult = partnerAddressService.getPartnerAddressByConditon(padListModel, partnerId,queryParam);
		jsonResult = new JSONObject();
		jsonResult.put(ConstantUtil.TOTALPROPERTY, partnerAddressResult.getTotal());
		jsonResult.put(ConstantUtil.ROOT, JSONArray.fromObject(partnerAddressResult.getResult()));
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private ListModel generateListModelParams() {
		ListModel listModel = new ListModel();
		listModel.setPageSize(limit == null ? ConstantUtil.PAGESIZE : limit);
		listModel.setPage(start ==null && limit == null ? 1: start / limit + 1);
		listModel.setOrderBy(ConstantUtil.PARAM_OF_ID);
		listModel.setDesc(true);
		return listModel;
	}
   /**
    * 保存客户收货地址
    * @author jiwei.yi
    * @return
    */
	public String save(){
		jsonResult = new JSONObject();
		try {
			PartnerAddress partnerAddress = (PartnerAddress) JSONObject.toBean(JSONObject.fromObject(queryParam),PartnerAddress.class);
			partnerAddressService.create(partnerAddress);
			jsonResult.put(ConstantUtil.MSG, ConstantUtil.INSERT_PARTNERADDRESS_SUCCESS);
			jsonResult.put(ConstantUtil.JSON_OF_MAIN, partnerAddress);
		} catch (Exception e) {
			log.error(ConstantUtil.INSERT_PARTNERADDRESS_ERROR, e);			
			jsonResult.put(ConstantUtil.MSG, ConstantUtil.INSERT_PARTNERADDRESS_ERROR);
		}		
		return SUCCESS;
	}
	
	public void setPartnerId(Integer partnerId) {
		this.partnerId = partnerId;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public void setPartnerAddressService(
			PartnerAddressService partnerAddressService) {
		this.partnerAddressService = partnerAddressService;
	}

	public JSONObject getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(JSONObject jsonResult) {
		this.jsonResult = jsonResult;
	}
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	

}
